<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" append-to-body :visible.sync="dialogVisible" width="500px" @close='close'>
    <el-form ref="form" :model="record" label-width="80px" label-position="left" :rules="rules" style="margin:0 30px;">
      <el-form-item label="IP" prop="ip">
        <el-input v-emoji v-model="record.ip" ref="publishIp" style="width:210px" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('前缀')" prop="mask">
        <el-input v-emoji v-model="record.mask" ref="publishMask" style="width:210px" size='small'></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="publishAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="publishAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  addBgpPublishRoute // Bgp添加发布路由
} from '@/api/network/route/dynamicRoute/BGP'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'PublishRouteDialog',
  data () {
    var validateIp = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入IP地址')))
      } else if (this.isValidIP(value)) {
        callback()
      } else if (this.isValidIPv6(value)) {
        callback()
      } else {
        callback(new Error(this.$t('请输入正确的IP地址')))
      }
    }
    var validateMask = (rule, value, callback) => {
      if (this.isValidIP(this.record.ip)) {
        if (!String(value).trim()) {
          callback(new Error(this.$t('请输入前缀')))
        } else if (!this.isInteger(value)) {
          callback(new Error(this.$t('只能输入整数')))
        } else if (Number(value) < 1 || Number(value) > 32) {
          callback(new Error(this.$t('ipv4前缀的范围是1-32')))
        } else {
          callback()
        }
      } else if (this.isValidIPv6(this.record.ip)) {
        if (!String(value).trim()) {
          callback(new Error(this.$t('请输入前缀')))
        } else if (!this.isInteger(value)) {
          callback(new Error(this.$t('只能输入整数')))
        } else if (Number(value) < 1 || Number(value) > 128) {
          callback(new Error(this.$t('ipv6前缀的范围是1-128')))
        } else {
          callback()
        }
      } else {
        callback(new Error(this.$t('请先输入正确的IP地址')))
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: '',
        ip: '',
        mask: ''
      },
      recordCopy: {
        id: '',
        ip: '',
        mask: ''
      },
      type: '',
      rules: {
        ip: [{ required: false, validator: validateIp, trigger: 'blur' }],
        mask: [{ required: false, validator: validateMask, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (type, id) {
      this.dialogVisible = true
      this.type = type
      this.record.id = id
      this.$nextTick(() => {
        const data = ['publishIp', 'publishMask']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let formData = JSON.parse(JSON.stringify(this.record))
          if (this.type === '编辑') {
            addBgpPublishRoute(formData).then(res => {
              if (res.status === 0) {
                this.$messages.success(this.$t('执行成功'))
                this.close()
                this.$emit('addPublishRoute', formData)
              }
            })
          } else {
            this.$emit('addPublishRoute', formData)
            this.close()
          }
        }
      })
    },
    close () {
      this.type = ''
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    },
    isValidIP (ip) {
      let reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      return reg.test(ip)
    },
    isValidIPv6 (ip) {
      let reg = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|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